"use strict";

var Favorite = Backbone.Model.extend( {
  TYPE_FAVORITE     : 'FAVORITE'
, TYPE_FOLDER       : 'FOLDER' 

, initialize                : function( element ) {
    this.set( {
      id        : element.node.id
    , index     : element.index
    , title     : element.node.title
    , url       : element.node.url
    , HTML      : null
    , type      : this.get( 'url' ) ? this.TYPE_FAVORITE : this.TYPE_FOLDER
    , collection: null
    } );
    
    console.log( 'Favorite.initialize', this.get( 'title' ) );
    
    if ( this.get( 'type' ) == this.TYPE_FAVORITE ) {
      this.buildFavoriteHTML();
    } else {
      this.buildFolderHTML();
      this.collection = new FavoritesCollection( null, { 
        bookmarkTreeNodeId  : this.get( 'id' )
      , parent              : this
      } ); 
    }
    
    //this.render();
    
    console.log( 'Favorite.type', this.get( 'HTML' ) );
      
  }
, buildFavoriteHTML         : function() {
  /*
    if ( !this.get( 'HTML' ) )
    var uri = this.get( 'url' );
    this.set( {
      HTML : $( '<a />', {
        'class' : 'recent-menu-item' 
      , href    : uri
      , style   : 'background-image: url(chrome://favicon/' + uri + ';">'
      } )
    } );
    
    console.log(this.get('HTML'));
  */
  }
, buildFolderHTML           : function() {
    var fragment  = document.createDocumentFragment();
    
    fragment.appendChild( $( '<section/>', {
      'class' : 'tableRow'
    , html    : $( '<h3/>', {
        'class' : 'tableCell tableHead'
      , text    : this.get( 'title' )
      } )
    } )[0] );
    
    fragment.appendChild( $( '<section/>', {
      'class' : 'tableRow'
    , html    : $( '<section/>', {
        'class' : 'tableCell'
      } )  
    } )[0] );   
    
    this.set( {
      HTML : fragment 
    } );
  }
, onChildCollectionComplete : function( evt ) {
    console.log( 'render called for ', this.get('title'), evt );
  }
} );